package com.by.controller;

import cn.hutool.core.bean.BeanUtil;
import com.by.bean.Management;
import com.by.common.bean.Login;
import com.by.common.bean.Page;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.common.group.ISave;
import com.by.common.group.V;
import com.by.service.ManagementService;
import com.by.token.TokenTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/management")
@Api(tags = "管理后台")
public class ManagementController extends BaseController {

    @Autowired
    TokenTemplate tokenTemplate;
    @Autowired
    ManagementService managementService;


//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "username", value = "用户名",dataTypeClass = String.class, required = true, paramType = "query"),
//            @ApiImplicitParam(name = "password", value = "密码", dataTypeClass = String.class,required = true, paramType = "query")
//    })
    @PostMapping("/login")
    @ApiOperation("管理员登录")
    public ResultBean login(@Validated @RequestBody Login login,BindingResult result) {
        V.validate(result);
        Management mag=managementService.queryByName(login.getUsername());
        if(null==mag)
            return fail("账户不存在");
        if(mag.getIsdel()==1)
            return fail("账户已经被删除");
        String pwd= DigestUtils.md5DigestAsHex(login.getPassword().getBytes());
        if(!pwd.equals(mag.getPassword()))
            return fail("密码错误");
        if (mag.getStatus()==1)
            return fail("账户被禁用");

        Map map= BeanUtil.beanToMap(mag);
        map.remove("password");

        String token=tokenTemplate.generateToken(map);
        map.put("token",token);
        return success("登录成功", map);
    }

    @ApiOperation("管理员查询")
    @GetMapping("/query")
    public ResultBean get(Page page) {
        PageBean pageBean = managementService.query(page.getPage(), page.getPageSize());
        return success("客户信息",pageBean);
    }


    @ApiOperation("管理员新增")
    @PostMapping("/update")
    public ResultBean save(@Validated(ISave.class) @RequestBody Management management, BindingResult result) {
        V.validate(result);
        int save = managementService.save(management);
        return save!=0?success("添加成功"):fail("添加失败");
    }

    @ApiOperation("管理员删除")
    @DeleteMapping("/delete")
    public ResultBean delete(String id) {
        int delete = managementService.delete(id);
        System.out.println(delete);
        return delete==1?success("删除成功 "):fail("删除失败");
    }

    @ApiOperation("管理员修改")
    @PutMapping("/update")
    public ResultBean update(@Validated(ISave.class) @RequestBody Management management, BindingResult result) {
        V.validate(result);
        int update = managementService.update(management);
        return update!=0?success("修改成功"):fail("修改失败");
    }


}
